package com.tensquare.user.controller;

import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import com.tensquare.user.annotation.PreAuthorize;
import com.tensquare.user.dto.ReqPermission;
import com.tensquare.user.service.PermissionService;
import com.tensquare.user.vo.Permission;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping(value = "per")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @RequestMapping(value = "/find", method = RequestMethod.GET)
    public Result findPermission(HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return new Result(true, "查询成功", StatusCode.OK, permissionService.findOne(userId));
    }

    @PreAuthorize(hasPermission = "per:update")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result updatePermission(@RequestBody Map<String, Object> map) {
        if (map.get("id") == null) {
            return new Result(false, "id不能为空", StatusCode.PARAMS_IN_ERROR);
        }
        permissionService.updatePer(map);
        return new Result(true, "修改成功", StatusCode.OK);
    }

    @PreAuthorize(hasPermission = "per:del")
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Result delPermission(@RequestBody Map<String, ArrayList<Integer>> map) {
        ArrayList<Integer> ids = map.get("ids");
        if (null == ids || ids.size() == 0) {
            return new Result(false, "id不能为空", StatusCode.PARAMS_IN_ERROR);
        }
        permissionService.delPer(ids);
        return new Result(true, "删除成功", StatusCode.OK);
    }

    @PreAuthorize(hasPermission = "per:add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result addPermission(@Valid @RequestBody ReqPermission permission) {
        if (permission.getParentId() == null) {
            return new Result(false, "父Id不能为空", StatusCode.PARAMS_IN_ERROR);
        }
        permissionService.addPer(permission);
        return new Result(true, "添加成功", StatusCode.OK);
    }

    @PreAuthorize(hasPermission = "per:list")
    @RequestMapping(value = "/list/{offset}/{limit}/{parent}", method = RequestMethod.GET)
    public Result findByPage(@PathVariable("offset") int offset, @PathVariable("limit") int limit, @PathVariable("parent") int isParent) {
        List<Permission> permissions = permissionService.findByPage(offset, limit, isParent);
        log.info("权限列表出参->{}", permissions.toString());
        return new Result(true, "查询成功", StatusCode.OK, permissions);
    }

    @PreAuthorize(hasPermission = "per:find")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result findAll() {
        return new Result(true, "查询成功", StatusCode.OK, permissionService.findAll());
    }

    @PreAuthorize(hasPermission = "per:list")
    @RequestMapping(value = "/list/{parentId}", method = RequestMethod.GET)
    public Result findByPage(@PathVariable("parentId") Integer parentId) {
        List<Permission> permissions = permissionService.findByParentId(parentId);
        log.info("权限列表出参->{}", permissions.toString());
        return new Result(true, "查询成功", StatusCode.OK, permissions);
    }

    /**
     * 查询角色的权限
     *
     * @param roleId 角色Id
     */
    @PreAuthorize(hasPermission = "per:find")
    @RequestMapping(value = "/role/{roleId}", method = RequestMethod.GET)
    public Result findPermissionByRoleId(@PathVariable Integer roleId) {
        List<Permission> permissions = permissionService.findByRoleId(roleId);
        log.info("权限列表出参->{}", permissions.toString());
        return new Result(true, "查询成功", StatusCode.OK, permissions);
    }

}
